package com.yanqu.road.server.manger.activity.springdinner;


import com.yanqu.road.entity.player.PlayerState;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.BulletScreenProto;
import com.yanqu.road.pb.activity.SpringDinnerProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.bulletscreen.BulletScreenModule;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.task.GameCommandTask;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.RandomHelper;

import java.util.List;

public class SpringDinnerDeBugMgr{

    private static RandomHelper randomHelper = new RandomHelper();

    private static int openTableNum = 5;
    private static int joinRandomNum = 16;

    public static void testTask(){
        for(int i = 0; i < openTableNum ; i++){
            openOneTable();
        }
        for(int i = 0; i < joinRandomNum ; i++){
            joinRandomTable();
        }
    }

    private static void openOneTable(){
        List<UserInfo> userInfoList = UserMgr.getUserInfoList();
        int index = randomHelper.next(0,userInfoList.size());
        UserInfo userInfo = userInfoList.get(index);
        GamePlayer player = GamePlayerMgr.getPlayer(userInfo.getUserId());
        if(player != null){
            SpringDinnerProto.CrossOpenRedPacketTableReqMsg.Builder msg = SpringDinnerProto.CrossOpenRedPacketTableReqMsg.newBuilder();
            msg.setActivityId(SpringDinnerMgr.getShowTimeActivityId());
            msg.setIntegral(Long.MAX_VALUE);
            msg.setMessage("测试开桌子");
            msg.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.parseUserBaseInfo(userInfo, GameServer.getInstance().getServerId())));
            YanQuMessage pb2Msg = YanQuMessageUtils.buildMessage(Protocol.C2_SPRINGDINNER_ACTIVITY_OPEN, msg);
            GamePlayerMgr.sendPacket(player.getUserId(), pb2Msg);
        }
    }

    private static void joinRandomTable(){
        List<UserInfo> userInfoList = UserMgr.getUserInfoList();
        int index = randomHelper.next(0,userInfoList.size());
        UserInfo userInfo = userInfoList.get(index);
        GamePlayer player = GamePlayerMgr.getPlayer(userInfo.getUserId());
        if(player != null){
            SpringDinnerProto.CrossRobRedPacketRandomReqMsg.Builder msg = SpringDinnerProto.CrossRobRedPacketRandomReqMsg.newBuilder();
            msg.setActivityId(SpringDinnerMgr.getShowTimeActivityId());
            msg.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.parseUserBaseInfo(userInfo, GameServer.getInstance().getServerId())));
            YanQuMessage pb2Msg = YanQuMessageUtils.buildMessage(Protocol.C2_SPRINGDINNER_ACTIVITY_ROB_RANDOM, msg);
            GamePlayerMgr.sendPacket(player.getUserId(), pb2Msg);

            //直接加次数吧，不然弹幕发不出
            player.getModule(BulletScreenModule.class).robAddCanBarrageTimes(SpringDinnerMgr.getShowTimeActivityId());
            YanQuMessage message = parseMessageMsg(player.getUserId());
            GameCommandTask cmdWrapper = new GameCommandTask(null, message, player);
            player.setState(PlayerState.ONLINE);
            player.getCommandQueue().add(cmdWrapper);
        }
    }


    private static YanQuMessage parseMessageMsg(long userId) {
        BulletScreenProto.BulletScreenContentReqMsg.Builder reqMsg = BulletScreenProto.BulletScreenContentReqMsg.newBuilder();
        reqMsg.setContent(userId + "的谢谢红包弹幕");
        reqMsg.setActivityId(SpringDinnerMgr.getShowTimeActivityId());
        return YanQuMessageUtils.buildMessageByte(Protocol.S_ACTIVITY_BULLET_SCREEN_COMMENTS, userId, reqMsg.build().toByteArray());
    }
}
